package com.test.controller;

import com.test.service.PropagationTestService;
import com.test.service.SimplePropagationTestService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * REQUIRED：支持当前事务，如果当前没有事务，就新建一个事务。这是最常见的选择。
 * SUPPORTS：支持当前事务，如果当前没有事务，就以非事务方式执行。
 * MANDATORY：支持当前事务，如果当前没有事务，就抛出异常。
 * REQUIRES_NEW：新建事务，如果当前存在事务，把当前事务挂起。
 * NOT_SUPPORTED：以非事务方式执行操作，如果当前存在事务，就把当前事务挂起。
 * NEVER：以非事务方式执行，如果当前存在事务，则抛出异常。
 * NESTED：支持当前事务，如果当前事务存在，则执行一个嵌套事务，如果当前没有事务，就新建一个事务。
 */
@RestController()
@RequestMapping("propagationTest")
public class PropagationTestController {

    @Resource
    private PropagationTestService propagationTestService;

    @Resource
    private SimplePropagationTestService simplePropagationTestService;

    @GetMapping("required")
    public void required() {
        System.out.println("=======controller required=====");
        simplePropagationTestService.required();
    }

    @GetMapping("supports")
    public void supports() {
        System.out.println("=======controller supports=====");
        simplePropagationTestService.supports();
    }

    /**
     * 没事务运行时会报错
     */
    @GetMapping("mandatory")
    public void mandatory() {
        System.out.println("=======controller mandatory=====");
        simplePropagationTestService.mandatory();
    }

    @GetMapping("requiresNew")
    public void requiresNew() {
        System.out.println("=======controller requiresNew=====");
        simplePropagationTestService.requiresNew();
    }

    @GetMapping("notSupported")
    public void notSupported() {
        System.out.println("=======controller notSupported=====");
        simplePropagationTestService.notSupported();
    }

    @GetMapping("never")
    public void never() {
        System.out.println("=======controller never=====");
        simplePropagationTestService.never();
    }

    @GetMapping("nested")
    public void nested() {
        System.out.println("=======controller nested=====");
        simplePropagationTestService.nested();
    }

    @GetMapping("noTxCallRequired")
    public void noTxCallRequired() {
        System.out.println("=======controller noTxCallRequired=====");
        propagationTestService.noTxCallRequired();
    }

    @GetMapping("noTxCallRequiredWithException")
    public void noTxCallRequiredWithException() {
        propagationTestService.noTxCallRequiredWithException();
    }

    @GetMapping("requiredCallRequired")
    public void requiredCallRequired() {
        propagationTestService.requiredCallRequired();
    }

    @GetMapping("requiredCallRequiredWithException")
    public void requiredCallRequiredWithException() {
        propagationTestService.requiredCallRequiredWithException();
    }

    @GetMapping("querySupports")
    public void querySupports() {
        System.out.println("=======controller querySupports=====");
        propagationTestService.querySupports();
    }

    @GetMapping("queryRequired")
    public void queryRequired() {
        System.out.println("=======controller queryRequired=====");
        propagationTestService.queryRequired();
    }

    @GetMapping("requiredCallNested")
    public void requiredCallNested() {
        System.out.println("=======controller requiredCallNested=====");
        propagationTestService.requiredCallNested();
    }

    @GetMapping("requiredCallNestedWithException")
    public void requiredCallNestedWithException() {
        System.out.println("=======controller requiredCallNestedWithException=====");
        propagationTestService.requiredCallNestedWithException();
    }

    @GetMapping("requiredCallNestedWithCatchException")
    public void requiredCallNestedWithCatchException() {
        System.out.println("=======controller requiredCallNestedWithCatchException=====");
        propagationTestService.requiredCallNestedWithCatchException();
    }

    @GetMapping("requiredCallRequiredNewWithCatchException")
    public void requiredCallRequiredNewWithCatchException() {
        System.out.println("=======controller requiredCallRequiredNewWithCatchException=====");
        propagationTestService.requiredCallRequiredNewWithCatchException();
    }

}
